package com.meepo.feed.topic

import android.graphics.Color
import android.os.Bundle
import android.view.View
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.interfaces.OnConfirmListener
import com.meepo.basic.components.listfragment.ListFragmentParams
import com.meepo.basic.router.RouterManager.FeedModule.FeedTopicDetail_Path
import com.meepo.framework.design.button.SlickButton
import com.meepo.framework.design.helper.ImageLoader
import com.meepo.framework.design.helper.StatusBarHelper
import com.meepo.feed.API
import com.meepo.feed.R
import com.meepo.basic.schema.feed.Topic
import com.meepo.basic.components.view.MeepoActivity
import com.meepo.feed.feed.FeedListFragment
import com.meepo.sdk.component.fragmentation.ISupportFragment
import com.xiaojinzi.component.anno.RouterAnno
import com.xiaojinzi.component.support.ParameterSupport
import kotlinx.android.synthetic.main.topic_detail_activity.*


@RouterAnno(hostAndPath = FeedTopicDetail_Path)
class TopicDetailActivity : MeepoActivity() {

    private lateinit var mTopicId: String
    private lateinit var mTopic: Topic

    private fun applyDetail(topic: Topic?) {

        topic?.let {


            mTopic = topic
//            tool_bar_title_tv.text = topic.title
//            tool_bar_title_tv.visibility = View.GONE

            ImageLoader.loadImage(this, topic.icon, cover_iv)
//            ImageLoader.loadImageBackground(this, topic.icon, collapsing_tool_bar)

            title_tv.text = topic.title
//            sub_title_tv.text = topic.desc

            // 加入按钮
            val unjoinedColor = this.resources.getColor(R.color.primary_color)
            val joinedColor = this.resources.getColor(R.color.primary_text_color)

            if (mTopic.isJoined()) {
                join_btn.text = "已加入"
                join_btn.setTextColor(joinedColor)
                join_btn.type = SlickButton.BUTTON_TYPE_DEFAULT
                join_btn.color = joinedColor
            } else {
                join_btn.text = "加入"
                join_btn.type = SlickButton.BUTTON_TYPE_PRIMARY
                join_btn.setTextColor(Color.WHITE)
                join_btn.color = unjoinedColor
            }

            join_btn.setOnClickListener {
                if (topic.isJoined()) {
                    XPopup.Builder(this)
                        .isDestroyOnDismiss(true)
                        .asConfirm(
                            "", """确定要退出话题 #${topic.title} 吗?""".trimIndent(),
                            "再想想", "退出",
                            OnConfirmListener {

                                presenter.call(
                                    API.instance().deleteTopicRelation(topic.id),
                                    { applyDetail(topic.setQuitted()) }
                                )

                            }, null, false, R.layout.topic_list_item_confirm_popup
                        )
                        .show()
                } else {

                    presenter.call(
                        API.instance().addTopicRelation(topic.id),
                        { applyDetail(topic.setJoined()) }
                    )
                }
            }

            // 描述
            desc_tv.text = topic.desc

            return
        }

        finish()
    }

    override fun initData() {

        presenter.call(
            API.instance().fetchTopicDetail(mTopicId),
            { t -> applyDetail(t.data) },
            { finish() }
        )
    }

    override fun initView(view: View, savedInstanceState: Bundle?) {
        mTopicId = ParameterSupport.getString(intent, "topicId", null)!!


        StatusBarHelper.immerse(this, tool_bar)

        back_iv.setOnClickListener { finish() }

        val fragment = FeedListFragment.instance(
            topicId = mTopicId,
            hideTopicView = true,
            listFragmentParams = ListFragmentParams(enablePullRefresh = false)
        )

        loadRootFragment(R.id.frame_layout, fragment as ISupportFragment)

    }

    override fun setUpContentLayout(): Int = R.layout.topic_detail_activity


}